﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.Validation.Interfaces;

namespace Kiiro.Validation.Validators.Entities
{
    public class UserValidator : BaseEntityValidator, IValidator<User>
    {
        private readonly List<ValidationMessage> validationErrors = new List<ValidationMessage> ( );

        #region  IValidator<User> Members
        public List<ValidationMessage> Validate ( User entityToValidate )
        {
            // If the entity is marked as deleted, don't do any validation, just return.
            if ( ( entityToValidate.EditType == EditType.Created || entityToValidate.EditType == EditType.Updated ) && !entityToValidate.IsADGroup )
            {
                ValidationMessage message = null;

                #region Required Fields Checks

                if ( entityToValidate.IsFormsUser )
                {
                    if ( entityToValidate.EditType == EditType.Created )
                    {
                        message = IsRequired ( entityToValidate.Password, "Password" );

                        if ( message != null )
                        {
                            entityToValidate.PasswordMessage.Add ( message );
                            validationErrors.Add ( message );
                        }

                        if (entityToValidate.Password != null)
                        {
                            message = PasswordLength(entityToValidate.Password.Length, "Password");

                            if (message != null)
                            {
                                entityToValidate.PasswordMessage.Add(message);
                                validationErrors.Add(message);
                            }
                        }

                        message = CompareLoginAndPassword ( entityToValidate.Login, entityToValidate.Password, "Password" );

                        if ( message != null )
                        {
                            entityToValidate.PasswordMessage.Add ( message );
                            validationErrors.Add ( message );
                        }
                    }
                }

                message = IsRequired(entityToValidate.Login, "Login");

                if (message != null)
                {
                    entityToValidate.LoginMessage.Add(message);
                    validationErrors.Add(message);
                }


                message = IsRequired ( entityToValidate.LastName, "Last Name" );

                if ( message != null )
                {
                    entityToValidate.LastNameMessage.Add ( message );
                    validationErrors.Add ( message );
                }

                message = IsRequired ( entityToValidate.Email, "Email" );

                if ( message != null )
                {
                    entityToValidate.EmailMessage.Add ( message );
                    validationErrors.Add ( message );
                }



                #endregion

                #region Comparison Checks

                if ( !string.IsNullOrEmpty ( entityToValidate.Title ) )
                {
                    message = CheckMaximumLength ( 100, entityToValidate.Title, "Title" );

                    if ( message != null )
                    {
                        entityToValidate.TitleMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.Title, "Title" );

                    if ( message != null )
                    {
                        entityToValidate.TitleMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.Description ) )
                {
                    message = CheckMaximumLength ( 1000, entityToValidate.Description, "Description" );

                    if ( message != null )
                    {
                        entityToValidate.DescriptionMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    //message = HasScriptingTagsInText ( entityToValidate.Description, "Description" );

                    //if ( message != null )
                    //{
                    //    entityToValidate.DescriptionMessage.Add ( message );
                    //    validationErrors.Add ( message );
                    //}
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.Login ) )
                {
                    message = CheckMaximumLength ( 255, entityToValidate.Login, "Login" );

                    if ( message != null )
                    {
                        entityToValidate.LoginMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.Login, "Login" );

                    if ( message != null )
                    {
                        entityToValidate.LoginMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.FirstName ) )
                {
                    message = CheckMaximumLength ( 100, entityToValidate.FirstName, "First Name" );

                    if ( message != null )
                    {
                        entityToValidate.FirstNameMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.FirstName, "First Name" );

                    if ( message != null )
                    {
                        entityToValidate.FirstNameMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.LastName ) )
                {
                    message = CheckMaximumLength ( 100, entityToValidate.LastName, "Last Name" );

                    if ( message != null )
                    {
                        entityToValidate.LastNameMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.LastName, "Last Name" );

                    if ( message != null )
                    {
                        entityToValidate.LastNameMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.PhoneNumber ) )
                {
                    message = CheckMaximumLength ( 255, entityToValidate.PhoneNumber, "Phone Number" );

                    if ( message != null )
                    {
                        entityToValidate.PhoneNumberMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.PhoneNumber, "Phone Number" );

                    if ( message != null )
                    {
                        entityToValidate.PhoneNumberMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.Email ) )
                {
                    message = IsEmailValid ( entityToValidate.Email, "Email" );

                    if ( message != null )
                    {
                        entityToValidate.EmailMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                    //else
                    {
                        message = CheckMaximumLength ( 100, entityToValidate.Email, "Email" );

                        if ( message != null )
                        {
                            entityToValidate.EmailMessage.Add ( message );
                            validationErrors.Add ( message );
                        }

                        message = HasScriptingTagsInText ( entityToValidate.Email, "Email" );

                        if ( message != null )
                        {
                            entityToValidate.EmailMessage.Add ( message );
                            validationErrors.Add ( message );
                        }
                    }
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.IM ) )
                {
                    message = CheckMaximumLength ( 100, entityToValidate.IM, "IM" );

                    if ( message != null )
                    {
                        entityToValidate.IMMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.IM, "IM" );

                    if ( message != null )
                    {
                        entityToValidate.IMMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.IM ) )
                {
                    message = CheckMaximumLength ( 255, entityToValidate.IMClient, "IM Client" );

                    if ( message != null )
                    {
                        entityToValidate.IMClientMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.IMClient, "IM Client" );

                    if ( message != null )
                    {
                        entityToValidate.IMClientMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                if (!string.IsNullOrEmpty(entityToValidate.CompanyName))
                {
                    message = CheckMaximumLength(255, entityToValidate.CompanyName, "Company Name");

                    if (message != null)
                    {
                        entityToValidate.CompanyNameMessage.Add(message);
                        validationErrors.Add(message);
                    }

                    message = HasScriptingTagsInText ( entityToValidate.CompanyName, "Company Name" );

                    if ( message != null )
                    {
                        entityToValidate.CompanyNameMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.StatusNote ) )
                {
                    message = CheckMaximumLength ( 1000, entityToValidate.StatusNote, "Status Note" );

                    if ( message != null )
                    {
                        entityToValidate.StatusNoteMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.StatusNote, "Status Note" );

                    if ( message != null )
                    {
                        entityToValidate.StatusNoteMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.PasswordSecurityAnswer ) )
                {
                    message = CheckMaximumLength ( 255, entityToValidate.PasswordSecurityAnswer, "Security Answer" );

                    if ( message != null )
                    {
                        entityToValidate.PasswordSecurityAnswerMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.PasswordSecurityAnswer, "Security Answer" );

                    if ( message != null )
                    {
                        entityToValidate.PasswordSecurityAnswerMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.PasswordSecurityQuestion ) )
                {
                    message = CheckMaximumLength ( 255, entityToValidate.PasswordSecurityQuestion, "Security Question" );

                    if ( message != null )
                    {
                        entityToValidate.PasswordSecurityQuestionMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.PasswordSecurityQuestion, "Security Question" );

                    if ( message != null )
                    {
                        entityToValidate.PasswordSecurityQuestionMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }

                if ( !string.IsNullOrEmpty ( entityToValidate.Password ) )
                {
                    message = CheckMaximumLength ( 255, entityToValidate.Password, "Password" );

                    if ( message != null )
                    {
                        entityToValidate.PasswordMessage.Add ( message );
                        validationErrors.Add ( message );
                    }

                    message = HasScriptingTagsInText ( entityToValidate.Password, "Password" );

                    if ( message != null )
                    {
                        entityToValidate.PasswordMessage.Add ( message );
                        validationErrors.Add ( message );
                    }
                }
                #endregion
                
            }


            return validationErrors;
        }

        #endregion

        private ValidationMessage CompareLoginAndPassword ( string valueLogin, string valuePassword, string propertyName )
        {
            ValidationMessage message = null;
            if ( string.Equals ( valueLogin, valuePassword ) )
            {
                message = new ValidationMessage ( ValidationSeverity.Validation, string.Format ( "Login and {0} cannot be the same.", propertyName ) );
            }
            return message;
        }

        private ValidationMessage PasswordLength ( int value, string propertyName )
        {
            ValidationMessage message = null;
            if ( value < 4 )
            {
                message = new ValidationMessage ( ValidationSeverity.Validation, string.Format ( "{0} cannot be less than 4 characters.", propertyName ) );
            }
            return message;
        }

        private ValidationMessage IsRequired ( byte[] value, string propertyName )
        {
            ValidationMessage message = null;
            if ( value == null )
            {
                message = new ValidationMessage ( ValidationSeverity.Validation, string.Format ( "{0} is a required field.", propertyName ) );
            }
            return message;
        }

        private ValidationMessage IsRequired ( SPUser value, string propertyName )
        {
            ValidationMessage message = null;
            if ( value == null )
            {
                message = new ValidationMessage ( ValidationSeverity.Validation, string.Format ( "{0} is a required field.", propertyName ) );
            }
            return message;
        }

        public ValidationMessage IsFormLoginUnique ( User value, string propertyName )
        {
            ValidationMessage message = null;
            if ( value != null )
            {
                message = new ValidationMessage ( ValidationSeverity.Validation, string.Format ( "{0} already exists.", propertyName ) );
                value.LoginMessage.Add ( message );
            }
            return message;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Validation/Validators/Entities/UserValidator.cs $
 * 
 * 9     10/26/09 10:42a Adamw
 * Case 332
 * 
 * 8     30/09/09 1:08p Camerons
 * Implementing UserModel.GetOrCreateUser(). 
 * 
 * 7     17/08/09 10:45a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/